package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.ResTryListenRecord;
import com.xmy.cultivate.mapper.*;
import com.xmy.cultivate.service.IEnrollInviteDetailService;
import com.xmy.cultivate.service.IQuarterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 邀约详情 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-08-22
 */
@Service
public class EnrollInviteDetailServiceImpl extends ServiceImpl<EnrollInviteDetailMapper, EnrollInviteDetail> implements IEnrollInviteDetailService {

    @Autowired
    EnrollInviteDetailMapper enrollInviteDetailMapper;

    @Autowired
    EnrollCustomerMapper enrollCustomerMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    RecordCourseMapper recordCourseMapper;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    OrderApplyMapper orderApplyMapper;


    /**
     * 更新招生状态
     * @param schoolDate
     * @return
     */
    @Override
    public boolean actionUpdateCustomer(LocalDate schoolDate) {
        this.updateStatusByRecordCourse(schoolDate);
        this.updateStatusByOrderApply(schoolDate);
        return true;
    }


    /**
     * 超过时间没有短信签到的改成待邀约
     */
    @Override
    public Boolean updateStatusByRecordCourse(LocalDate schoolDate) {
        String startDateTime = schoolDate.toString()+" 00:00:00";
        String endDateTime = schoolDate.toString()+" 23:59:59";
        List<EnrollInviteDetail> enrollInviteDetails = enrollInviteDetailMapper.getEnrollInviteDetailList(startDateTime,endDateTime);
        Quarter quarter = iQuarterService.getNowQuarter();
        for (EnrollInviteDetail enrollInviteDetail : enrollInviteDetails) {
            List<ResTryListenRecord> tryListenRecordList = enrollInviteDetailMapper.getTryListenList(quarter.getYearPart(),quarter.getNum(),schoolDate.toString(),enrollInviteDetail.getPhone());
            if (tryListenRecordList.size() == 0) {
                UpdateWrapper<EnrollCustomer> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("status", 2);
                updateWrapper.eq("id", enrollInviteDetail.getCustomerId());
                updateWrapper.set("status",0);
                enrollCustomerMapper.update(null,updateWrapper);
            }
        }
        return true;
    }

    /**
     * 当天未录入成交报名状态改为未成交
     */
    @Override
    public Boolean updateStatusByOrderApply(LocalDate schoolDate) {
        String startDateTime = schoolDate.toString()+" 00:00:00";
        String endDateTime = schoolDate.toString()+" 23:59:59";

        List<EnrollInviteDetail> enrollInviteDetails = enrollInviteDetailMapper.getEnrollInviteDetailList(startDateTime,endDateTime);
        Quarter quarter = iQuarterService.getNowQuarter();
        for (EnrollInviteDetail enrollInviteDetail : enrollInviteDetails) {
            List<ResTryListenRecord> tryListenRecordList = enrollInviteDetailMapper.getTryListenList(quarter.getYearPart(),quarter.getNum(),schoolDate.toString(),enrollInviteDetail.getPhone());
            for (ResTryListenRecord retryListenRecord : tryListenRecordList) {
                // 有试听记录，查看是否有报名
                Student student = studentMapper.selectById(retryListenRecord.getStudentId());
                if (student!=null){
                    Integer orderApplyCount = orderApplyMapper.getOrderApplyByDateAndStudentId(quarter.getYearPart(), quarter.getNum(),startDateTime,endDateTime,enrollInviteDetail.getPhone());
                    if (orderApplyCount.equals(0)) {
                        UpdateWrapper<EnrollCustomer> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.eq("status", 2);
                        //updateWrapper.eq("phone", enrollInviteDetail.getPhone());
                        updateWrapper.eq("id", enrollInviteDetail.getCustomerId());
                        updateWrapper.set("status",3);
                        enrollCustomerMapper.update(null,updateWrapper);
                    }
                }
            }
        }
        return true;
    }
}
